{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cf954d89",
   "metadata": {},
   "source": [
    "***\n",
    "# 上周回顾\n",
    "\n",
    "1. 人脸识别概念和基础\n",
    "2. 人脸识别应用\n",
    "3. 人脸识别实践——face++\n",
    "\n",
    "> 1. API文档阅读规范\n",
    ">\n",
    "> 2. 人脸检测（人脸分析)实践\n",
    ">\n",
    "> 3. 输入/输出\n",
    ">\n",
    "> 4. 错误码寻找原因\n",
    "\n",
    "****\n",
    "\n",
    "# 本周内容\n",
    "\n",
    "> 1. 人脸对比\n",
    ">\n",
    "> 2. 人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "56ee33e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679283136,7e01056a-9a04-485d-b9cf-c8f66a837f18', 'time_used': 359, 'confidence': 86.986, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'd5e5c2879ab259ab0d896ffe5dd40e8e', 'face_rectangle': {'top': 62, 'left': 100, 'width': 84, 'height': 84}}], 'faces2': [{'face_token': '205b381aa0d8ad540140f18781ee38ff', 'face_rectangle': {'top': 265, 'left': 218, 'width': 232, 'height': 232}}], 'image_id1': 'I5NRLkZdShzeXCEfT3LpUg==', 'image_id2': 'RFev/v6wiHoR0DLT6eEcNw=='}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "''' \n",
    "\n",
    "FACE++ 人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f_01= open('11.jpg', 'rb')\n",
    "f_02= open('22.jpg', 'rb')\n",
    "img_01= base64.b64encode(f_01.read())  \n",
    "img_02= base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "    \"image_base64_1\":img_01,\n",
    "    \"image_base64_2\":img_02,\n",
    "     'api_key':'1fhU_tz8rD0DOy0n-mvIwU0KC2XHAL8g',\n",
    "    'api_secret':'tQVlmCPZZrl9fnht5D1OCdIxgXiJp4_l',\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ea5a9cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 人脸搜索\n",
    " 2.1 人脸库管理\n",
    "    2.1.1 创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e04b90b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': '4f72e4432431959f529a90302803aba1', 'time_used': 150, 'face_count': 0, 'face_added': 0, 'request_id': '1679283107,8b4774e6-c52f-4ee6-85f9-851d98d51f9b', 'outer_id': '', 'failure_detail': []}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "''' \n",
    "\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\n",
    "     'api_key':'1fhU_tz8rD0DOy0n-mvIwU0KC2XHAL8g',\n",
    "    'api_secret':'tQVlmCPZZrl9fnht5D1OCdIxgXiJp4_l',\n",
    "    \"display_name\":\"广州南方学院东20\"\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ccf0b91",
   "metadata": {},
   "outputs": [],
   "source": [
    "2.1.2 检查人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "edf7642a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '4f72e4432431959f529a90302803aba1',\n",
       " 'tags': '',\n",
       " 'time_used': 127,\n",
       " 'user_data': '',\n",
       " 'display_name': '广州南方学院东20',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1679283504,61820a46-c622-426d-8951-0c1441eb821e',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "''' \n",
    "\n",
    "人脸数据库集合详情查询\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params = {\n",
    "     'api_key':'1fhU_tz8rD0DOy0n-mvIwU0KC2XHAL8g',\n",
    "    'api_secret':'tQVlmCPZZrl9fnht5D1OCdIxgXiJp4_l',\n",
    "    \"faceset_token\":\"4f72e4432431959f529a90302803aba1\"\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print(response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5b43a93",
   "metadata": {},
   "source": [
    "## 增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "aae2a347",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '东20/bb.jpg'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Input \u001b[1;32mIn [33]\u001b[0m, in \u001b[0;36m<cell line: 13>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     11\u001b[0m request_url \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttps://api-cn.faceplusplus.com/facepp/v3/detect\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     12\u001b[0m \u001b[38;5;66;03m# 二进制打开图片文学\u001b[39;00m\n\u001b[1;32m---> 13\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m东20/bb.jpg\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mrb\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     15\u001b[0m params \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m     16\u001b[0m      \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mapi_key\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m1fhU_tz8rD0DOy0n-mvIwU0KC2XHAL8g\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m     17\u001b[0m     \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mapi_secret\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtQVlmCPZZrl9fnht5D1OCdIxgXiJp4_l\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m     18\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfaceset_token\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m4f72e4432431959f529a90302803aba1\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     19\u001b[0m     \n\u001b[0;32m     20\u001b[0m }\n\u001b[0;32m     22\u001b[0m response \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mpost(request_url, data\u001b[38;5;241m=\u001b[39mparams)\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '东20/bb.jpg'"
     ]
    }
   ],
   "source": [
    "# 先检测人脸信息\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "''' \n",
    "\n",
    "人脸检测：杨颖,范冰冰\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制打开图片文学\n",
    "f = open('东20/bb.jpg', 'rb')\n",
    "\n",
    "params = {\n",
    "     'api_key':'1fhU_tz8rD0DOy0n-mvIwU0KC2XHAL8g',\n",
    "    'api_secret':'tQVlmCPZZrl9fnht5D1OCdIxgXiJp4_l',\n",
    "    \"faceset_token\":\"4f72e4432431959f529a90302803aba1\"\n",
    "    \n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print(response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd993565",
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "''' \n",
    "\n",
    "人脸数据库集合详情查询\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params = {\n",
    "     'api_key':'1fhU_tz8rD0DOy0n-mvIwU0KC2XHAL8g',\n",
    "    'api_secret':'tQVlmCPZZrl9fnht5D1OCdIxgXiJp4_l',\n",
    "    \"faceset_token\":\"4f72e4432431959f529a90302803aba1\"\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print(response.status_code)\n",
    "response.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
